
//
//  AutoLoginManager.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/19.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "AutoLoginManager.h"
#import "NetRequest+user.h"
#import "UserManager.h"

@implementation AutoLoginManager

+ (void)autoLogin:(NSString *)urlScheme paramsDictionary:(id)paramsDict   success:(void (^)(id data, BOOL isSuccess, RequestError *msg))success
             fail:(void (^)(RequestError *errMsg))fail {
    NSLog(@"开始自动登录");
    NSString *emailAddress = [kUserDefaults objectForKey:LOGIN_EmailAddress];
    NSString *pwd = [kUserDefaults objectForKey:LOGIN_PASSWord];
    
    NSURLSessionDataTask *dataTask = [[LoginManager sharedInstance] loginDictorany:@{LOGIN_EmailAddress:emailAddress,LOGIN_PASSWord:pwd} success:^(UserMode *user) {
        //成功之后继续请求前一个
        [[NetRequest sharedInstance] postDataFromURLScheme:urlScheme paramsDictionary:paramsDict success:^(id data, BOOL isSuccess, RequestError *msg) {
            if (success) {
                success(data,isSuccess,msg);
            }
        } fail:^(RequestError *errMsg) {
            if (fail) {
                fail(errMsg);
            }
        }];
    } failed:^(RequestError *error) {
        //登出
        [[UserManager shareInstance] logout];
        NSLog(@"自动登录失败");
        if (fail) {
            fail(error);
        }
    }];
}

@end
